package org.code.chenwei.day02.ThreadLocal;


import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject: learn-learning-everyday
 * @BelongsPackage: org.chenwei.day02.ThreadLocal
 * @Author: Chen Wei
 * @CreateTime: 2024-01-12  14:38
 * @Description: TODO
 * @Version: 1.0
 */
@SuppressWarnings("all")
@Slf4j
public class ThreadLocalTest {

    class User {
        private int age;
        private byte[] aByte = new byte[1024 * 1024];

    }

    //强引用
    @Test
    public void ThreadLocalDemo01() {
        ExecutorService executorService = Executors.newCachedThreadPool();
        ThreadLocal<User> threadLocal = new ThreadLocal<>();
        for (int i = 0; i < 30; i++) {
            executorService.execute(() -> {
                threadLocal.set(new User());
//               threadLocal.remove();
            });
        }
        SoftReference<User> softReference = new SoftReference(new User());
        User user = softReference.get();

        WeakReference<User> weakReference = new WeakReference<>(new User());

        try {
            executorService.awaitTermination(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }    //软引用

    @Test
    public void ThreadLocalDemo02() {
        ExecutorService executorService = Executors.newCachedThreadPool();
        ThreadLocal<SoftReference> threadLocal = new ThreadLocal<>();
        for (int i = 0; i < 100; i++) {
            executorService.execute(() -> {
                SoftReference<User> softReference = new SoftReference(new User());
                User user = softReference.get();
                threadLocal.set(softReference);
//               threadLocal.remove();
            });
        }


        WeakReference<User> weakReference = new WeakReference<>(new User());

        try {
            executorService.awaitTermination(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }    //弱引用

    @Test
    public void ThreadLocalDemo03() {
        ExecutorService executorService = Executors.newCachedThreadPool();
        ThreadLocal<WeakReference> threadLocal = new ThreadLocal<>();
        for (int i = 0; i < 30; i++) {
            executorService.execute(() -> {
                WeakReference<User> weakReference = new WeakReference<>(new User());
                threadLocal.set(weakReference);
//               threadLocal.remove();
            });
        }


        try {
            executorService.awaitTermination(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }

    @Test
    public void parentThreadAndChildThread() {
        ThreadLocal<String> threadLocal=new ThreadLocal<>();
        threadLocal.set("test");
        Thread thread=new Thread(()->{
            String value = threadLocal.get();
            System.out.println("value="+value);
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
